package solitaire.controle;

import java.awt.datatransfer.DataFlavor;

import solitaire.InterfacesControle.ICCarte;
import solitaire.InterfacesControle.ICTas;
import solitaire.InterfacesControle.ICTasDeCartes;
import solitaire.InterfacesControle.ICTasDeCartesAlternees;
import solitaire.InterfacesPresentation.IPTas;
import solitaire.InterfacesPresentation.IPTasDeCartesAlternees;
import solitaire.application.Carte;
import solitaire.application.TasDeCartesAlternees;
import solitaire.application.Usine;
import solitaire.presentation.PTasDeCartesAlternees;

public class CTasDeCartesAlternees extends TasDeCartesAlternees implements ICTasDeCartesAlternees{
	protected IPTasDeCartesAlternees presentation;

	public CTasDeCartesAlternees(String nom, Usine usine) {
		super(nom, usine);
		presentation=new PTasDeCartesAlternees(this);
		this.usine=new CUsine(usine);
	}

	public void empiler(Carte cartet){
		ICCarte carte=(ICCarte) cartet;
		super.empiler(carte.getAbstraction());
		presentation.empiler(carte.getPresentation());
	}


	@Override
	public boolean isEmpilable(ICCarte carte) {
		return super.isEmpilable(carte.getAbstraction());
	}

	@Override
	public void recopier(ICTas tas) {
		super.recopier(tas.getAbstraction());
		//presentation.viderTas();
		//presentation.empiler(tas.copy().getPresentation());

	}

	@Override
	public ICCarte getICCarte(int n) {
		ICCarte carte=null;
		try {
			carte = (ICCarte) super.getCarte(n);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return carte;
	}

	@Override
	public ICCarte getICCartebase() {
		ICCarte carte=null;
		try {
			carte = (ICCarte) super.getBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return carte;
	}

	@Override
	public ICCarte getICCarteSommet() {
		ICCarte carte=null;
		try {
			carte = (ICCarte) super.getSommet();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return carte;
	}

	public void depiler(){
		try {
			presentation.getJComponent().remove(this.getICCarteSommet().getPresentation().getJPanel());
			super.depiler();
			presentation.depiler();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public TasDeCartesAlternees getAbstraction() {
		return this;
	}

	@Override
	public IPTas getPresentation() {
		return (IPTas) presentation;
	}

	@Override
	public void depilerICCarte() {
		try {
			super.depiler();
		} catch (Exception e) {
			e.printStackTrace();
		}
		presentation.depiler();
		
	}

	@Override
	public ICCarte getICCarteBase() {
		ICCarte carte=null;
		try {
			carte=(ICCarte) super.getBase();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return carte;
	}

	@Override
	public boolean isEmpilable(ICTas tas) {
		return super.isEmpilable(tas.getAbstraction());
	}

	@Override
	public boolean isEmpilable(ICTas tas, int n) {
		return super.isEmpilable(tas.getAbstraction(), n);
	}

	@Override
	public ICTas copy() {
		ICTas tas=null;
		try {
			tas= (ICTas) this.clone();
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return tas;
	}

	@Override
	public boolean isEmpilable(ICTasDeCartes tas) {
		return super.isEmpilable(tas.getAbstraction());
	}

	@Override
	public boolean isEmpilable(ICTasDeCartes tas, int nb) {
		return super.isEmpilable(tas.getAbstraction(), nb);
	}

	@Override
	public Object getTransferData(DataFlavor flavor){
		Object result=null;
		if(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)){
			result=this;
		}
		return result;
	}

	@Override
	public DataFlavor[] getTransferDataFlavors() {
		DataFlavor data[] = new DataFlavor[1];
		try{
			data[0] = new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType);
		} catch (java.lang.ClassNotFoundException e){}
		return(data);
	}

	@Override
	public boolean isDataFlavorSupported(DataFlavor flavor) {
		boolean result=false;
		if(flavor.isMimeTypeEqual(DataFlavor.javaJVMLocalObjectMimeType)){
			result=true;
		}
		return result;
	}

	@Override
	public void empiler(ICTas tas) {
		super.empiler(tas.getAbstraction());
	}


}
